package com.example.administrator.listview_sqlite2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;

public class StuAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Student>studentArrayList;

    public StuAdapter(Context context, ArrayList<Student> studentArrayList) {
        this.context = context;
        this.studentArrayList = studentArrayList;
    }

    @Override
    public int getCount() {
        return studentArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return studentArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    /*为空时存放进去，不为空是把他直接取出来*/
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view==null){
            view= LayoutInflater.from(context).inflate(R.layout.stu_item,viewGroup,false);
            StuviewHoder stuviewHoder=new StuviewHoder();
            stuviewHoder.txtno=view.findViewById(R.id.txt_no);
            stuviewHoder.txtname=view.findViewById(R.id.txt_name);
            stuviewHoder.txtage=view.findViewById(R.id.txt_age);
            view.setTag(stuviewHoder);

        }
        Student student=studentArrayList.get(i);
        StuviewHoder stuviewHoder=(StuviewHoder) view.getTag();
        stuviewHoder.txtno.setText(student.getNo());
        stuviewHoder.txtname.setText(student.getName());
        stuviewHoder.txtage.setText(String.valueOf(student.getAge()));

        return view;
    }
}
